package tree;

/**
 * @Author wangtengyu
 * @Create 2018-09-12-10:47 判断是否是对称二叉树
 */
public class SymmetricTree {


  public boolean isSymmetricTree(TreeNode root) {
    if (root == null) {
      throw new RuntimeException("树为null");
    }
    return isSymmetric(root, root);
  }

  private boolean isSymmetric(TreeNode root1, TreeNode root2) {

    //定义出口，相等再一直往下进行判断不是出口
    if (root1 == null && root2 == null) {
      return true;
    }
    //只有一个为null
    if (root1 == null || root2 == null) {
      return false;
    }

    if (root1.getValue() != root2.getValue()) {
      return false;
    }

    return isSymmetric(root1.getLeft(), root2.getRight()) && isSymmetric(root1.getRight(),
        root2.getLeft());
  }


  public static void main(String[] args) {
    TreeNode root = new TreeNode('8');
    root.setLeft(new TreeNode('6'));
//        root.getLeft().setLeft(new TreeNode('5'));
    root.getLeft().setRight(new TreeNode('7'));

    root.setRight(new TreeNode('6'));
    root.getRight().setLeft(new TreeNode('7'));
//        root.getRight().setRight(new TreeNode('5'));

    SymmetricTree symmetricTree = new SymmetricTree();
    boolean isSymmetric = symmetricTree.isSymmetricTree(root);
    if (isSymmetric) {
      System.out.println("该树是对称的");
    } else {
      System.out.println("该数不是对称的");
    }
  }

}
